package net.gyapp.markim.module.contact.adapter;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import net.gyapp.markim.R;
import net.gyapp.markim.core.reponse.LoginResponse;
import net.gyapp.markim.module.basic.base.BaseRecyclerHolder;
import net.gyapp.markim.module.contact.vo.ContactHeaderEntity;

/**
 * Author:  winds
 * Data:    2018/7/7
 * Version: 1.0
 * Desc:
 */


public class ContactAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseRecyclerHolder> {


    public final static int TYPE_CONTACT_HEADER = 3001;
    public final static int TYPE_CONTACT_USER = 3002;
    public final static int TYPE_CONTACT_GROUP = 3003;

    public ContactAdapter() {
        super(null);
        addItemType(TYPE_CONTACT_HEADER, R.layout.item_contact_cate);
        addItemType(TYPE_CONTACT_USER, R.layout.item_contact_content);
        addItemType(TYPE_CONTACT_GROUP, R.layout.item_contact_content);
    }

    @Override
    protected void convert(BaseRecyclerHolder helper, MultiItemEntity item) {
        switch (item.getItemType()) {
            case TYPE_CONTACT_HEADER:
                convertHeader(helper, (ContactHeaderEntity) item);
                break;
            case TYPE_CONTACT_USER:
                convertUser(helper, (LoginResponse.DataBean.UserBean.UserChildBean) item);
                break;
            case TYPE_CONTACT_GROUP:
                convertGroup(helper, (LoginResponse.DataBean.UserBean.GroupsBean) item);
                break;
        }
    }

    private void convertUser(BaseRecyclerHolder helper, LoginResponse.DataBean.UserBean.UserChildBean item) {
        helper.setImageManager(mContext, R.id.iv_header, item.avatar, R.drawable.me_pressed);
        helper.setText(R.id.tv_name, item.nick);
    }

    private void convertGroup(BaseRecyclerHolder helper, LoginResponse.DataBean.UserBean.GroupsBean item) {
        helper.setText(R.id.tv_name, item.name);
//        helper.setImageManager(mContext, R.id.iv_header, item.name);
    }

    private void convertHeader(BaseRecyclerHolder helper, ContactHeaderEntity item) {
        helper.setText(R.id.tv_cate, item.name);
    }

    public void setContactData(LoginResponse.DataBean.UserBean user) {
        mData.clear();
        mData.add(new ContactHeaderEntity("群聊", TYPE_CONTACT_GROUP));
        if (user.groups != null) {
            mData.addAll(user.groups);
        }
        mData.add(new ContactHeaderEntity("好友", TYPE_CONTACT_GROUP));
        if (user.friends != null && user.friends.size() > 0) {
            if (user.friends.get(0).users != null) {
                mData.addAll(user.friends.get(0).users);
            }
        }
        notifyDataSetChanged();
    }
}
